#!/bin/bash

# Tidy up terminal, its on OCD thing!!!
clear

# Path for backups to be moved to, NO trailing slash
# e.g. backupPath='/home/user/myfolder
# Default is current directory (pwd)
backupPath=$(pwd)

# Load list of databases on local server into an array
array=($(mysql -e "show databases;"))

# Loop through array and list databases with index number to left
count=${#array[@]}
index=0
while [ "$index" -lt "$count" ]; do
    echo -e "index: $index\tvalue: ${array[$index]}"
    let "index++"
done

# get user selection and store database name
echo Please choose database you would like to backup
read dbChoice
myDB=${array[$dbChoice]}

echo "OK backing up $myDB to $backupPath"


# Do a mysql dump, zip and move
rm -rf backup.sql*
mysqldump --lock-tables=false -u root $myDB > backup.sql
gzip backup.sql
mv backup.sql.gz $backupPath/`/bin/date +$myDB.sql-$(date +%Y-%m-%d-%H%M).gz`

